import Vue from 'vue'
import VueRouter from 'vue-router'
import SystemHome from '@/views/SystemHome/SystemHome'
import User from '@/views/User/User'
import Article from '@/views/Article/Article'
import Login from '@/views/Login/Login.vue'
import EditUserInfo from '@/views/User/EditUserInfo'
import InsertUserInfo from '@/views/User/InsertUserInfo'
import InsertArticle from '@/views/Article/InsertArticle'
import EditArticleInfo from '@/views/Article/EditArticleInfo'
import BlogHome from '@/views/BlogHome/BlogHome'
import ArticlePage from '@/views/BlogPage/ArticlePage'
import TagsPage from '@/views/BlogPage/TagsPage'
import ArtitalkPage from '@/views/BlogPage/ArtitalkPage'
import MySpace from '@/views/BlogPage/MySpace'
import Publish from '@/views/BlogPage/Publish'
import TagStatistics from '@/views/Statistics/TagStatistics.vue'
import UserStatistics from '@/views/Statistics/UserStatistics'

import pathArr from '@/router/pathArr'

Vue.use(VueRouter)

const routes = [
  // 博客前台路由
  {
    path: '/',
    name: 'BlogHome',
    component: BlogHome
  },
  {
    path: '/blog/article/:article_id',
    name: 'ArticlePage',
    component: ArticlePage,
    props: true
  },
  {
    path: '/blog/tagspage',
    name: 'TagsPage',
    component: TagsPage
  },
  {
    path: '/blog/artitalkpage',
    name: 'ArtitalkPage',
    component: ArtitalkPage
  },
  {
    path: '/blog/myspace',
    name: 'MySpace',
    component: MySpace
  },
  {
    path: '/blog/publish',
    name: 'Publish',
    component: Publish
  },
  // 管理系统路由
  {
    path: '/system',
    name: 'Login',
    component: Login
  },
  {
    path: '/system/home',
    name: 'SystemHome',
    component: SystemHome,
    redirect: '/system/home/user',
    children: [
      {
        path: '/system/home/user',
        name: 'User',
        component: User
      },
      {
        path: '/system/home/edituserinfo',
        name: 'EditUserInfo',
        component: EditUserInfo
      },
      {
        path: '/system/home/insertuserinfo',
        name: 'InsertUserInfo',
        component: InsertUserInfo
      },
      {
        path: '/system/home/article',
        name: 'Article',
        component: Article
      },
      {
        path: '/system/home/insertarticle',
        name: 'InsertArticle',
        component: InsertArticle
      },
      {
        path: '/system/home/editarticle/:id',
        name: 'EditArticleInfo',
        component: EditArticleInfo,
        props: true
      },
      {
        path: '/system/home/tagstatistics',
        name: 'TagStatistics',
        component: TagStatistics
      },
      {
        path: '/system/home/userstatistics',
        name: 'UserStatistics',
        component: UserStatistics
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/')
    }
  } else {
    next()
  }
})

export default router
